home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Code / Chapter Code.exe / Chapter09 / FlexibleSpeedPanel.java < prev    next >
Text File  |  2000-09-09  |  2KB  |  84 lines

  1.  
  2. package applets;
  3.  
  4. import shout3d.*;
  5. import shout3d.core.*;
  6. import shout3d.math.*;
  7.  
  8.  
  9. public class FlexibleSpeedPanel extends Shout3DPanel implements DeviceObserver {
  10.    
  11.    
  12.    float speed = 0.0f; //percentage per second
  13.    TimeSensor timer;
  14.    double timeLength;
  15.    BezierVecInterpolator interp;
  16.    float fraction = 0;
  17.    
  18.    //screen pixels  
  19.    int pixelStartY;
  20.    int pixelEndY;
  21.  
  22.  
  23.    public FlexibleSpeedPanel (Shout3DApplet applet){
  24.       super(applet);
  25.    }
  26.    
  27.    
  28.    public void customInitialize() {
  29.       getRenderer().addRenderObserver(this, null);
  30.       addDeviceObserver(this,"MouseInput", null);
  31.         timer =(TimeSensor) getNodeByName("world-TIMER");
  32.       timeLength = timer.cycleInterval.getValue();
  33.         //use this to set speed factor
  34.         
  35.        interp =(BezierVecInterpolator) getNodeByName("Cone01-BEZPOS-INTERP");    
  36.    }
  37.  
  38.  
  39.    protected void finalize()  { 
  40.       getRenderer().removeRenderObserver(this);
  41.       removeDeviceObserver(this,"MouseInput");
  42.    }
  43.  
  44.    public boolean onDeviceInput(DeviceInput di, Object userData) {
  45.       MouseInput mi = (MouseInput) di;
  46.       switch (mi.which){
  47.  
  48.          case MouseInput.DOWN:
  49.  
  50.             pixelStartY = mi.y;
  51.             return true;
  52.             
  53.          case MouseInput.UP:
  54.             speed = 0.0f;
  55.             return true;
  56.  
  57.          case MouseInput.DRAG:
  58.  
  59.             int pixelEndY = mi.y;
  60.             int dragDistanceY = pixelEndY - pixelStartY;
  61.             
  62.             //convert drag to speed
  63.             //at 70 pixels per meter/second
  64.             speed = 33.33f/dragDistanceY;
  65.             return true;
  66.          
  67.       }//end of switch
  68.  
  69.       return false;
  70.     }
  71.  
  72.  
  73.    public void onPreRender (Renderer r, Object o) {
  74.       
  75.       float fractionDelta = speed/getFramesPerSecond();
  76.       fraction = fraction + fractionDelta;
  77.       interp.fraction.setValue(fraction);
  78.       interp.update();
  79.  
  80.    
  81.    }
  82.     
  83.  
  84. } //end of class